package edu.lingnan.springcloud.controller;

import edu.lingnan.springcloud.entity.Classes;
import edu.lingnan.springcloud.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 班级表(Classes)表控制层
 *
 * @author makejava
 * @since 2020-06-07 23:54:40
 */
@RestController
@RequestMapping("classes")
public class ClassesController {
    /**
     * 服务对象
     */
    @Resource
    private ClassesService classesService;

    /**
     * 获取配置信息，得到具体的微服务
     */
    @Autowired
    private DiscoveryClient client;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/get/{id}")
    public Classes selectOne(@PathVariable("id") Integer id) {
        return this.classesService.queryById(id);
    }

    @GetMapping("list")
    public List<Classes> list(){
        return this.classesService.queryAll();
    }

    /**
     * 注册进来的微服务，获取信息
     */
    @GetMapping("discover")
    public Object discovery(){
//        获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("DiscoveryClient:::" + services);

//      得到一个具体的微服务信息，通过具体的微服务ID，Application；
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-CLASSES");

        for (ServiceInstance instance :
                instances) {
            System.out.println(
                    instance.getHost() + "\t" +
                    instance.getPort() + "\t" +
                    instance.getUri() + "\t" +
                    instance.getServiceId()
            );
        }
        return this.client;

    }

}